import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../config/service_method.dart';
import 'dart:convert';
import '../../common/ok_toast.dart';

class ExamResult extends StatefulWidget {
    
    ExamResult({this.analysisId,this.schoolYearStart,this.roleId});
    final String analysisId;
    final String schoolYearStart;
    final String roleId;
    @override
    _ExamResultState createState() => _ExamResultState();
}

class _ExamResultState extends State<ExamResult> {

    String examName;
    // List examResultDetail = [{"subjectName":"lll","result":"llkjkj"}];
    var _futureBuilderGetExamResultDetail;

    @override
      void initState() {
        super.initState();
        getExamName();
        _futureBuilderGetExamResultDetail = getExamResultDetail();
        
      }


    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: NestedScrollView(
            headerSliverBuilder: _sliverBuilder,
            body: FutureBuilder(
                future: _futureBuilderGetExamResultDetail,
                builder: (context,snapshot){
                    if(snapshot.connectionState.toString() == "ConnectionState.done"){
                        return ListView.builder(
                            itemCount: snapshot.data.length,
                            itemBuilder: (BuildContext context,int index){
                                return Container(
                                    margin: EdgeInsets.all(5.0),
                                    padding: EdgeInsets.all(5.0),
                                    decoration: BoxDecoration(
                                        border: Border.all(color: Colors.grey, width: 1.0),
                                        borderRadius: BorderRadius.circular(3.0)
                                    ),
                                    child: Column(
                                        children: <Widget>[
                                            Row(
                                                children: <Widget>[
                                                    Expanded(
                                                        child: Container(
                                                            margin: EdgeInsets.only(left: 10.0),
                                                            child: Text(
                                                              snapshot.data[index]["subjectName"],
                                                              style: TextStyle(
                                                                fontSize: 18.0
                                                              ),
                                                            ),
                                                        )
                                                    ),
                                                    Container(
                                                        margin: EdgeInsets.only(right: 10.0),
                                                        child: Text(
                                                            snapshot.data[index]["result"],
                                                            style: TextStyle(
                                                              fontSize: 18.0
                                                            ),
                                                        ),
                                                    )
                                                ],
                                            ),
                                            Container(
                                                          child: Divider(height: 10.0,),
                                                        ),
                                            Row(
                                                children: <Widget>[
                                                    Expanded(
                                                        child: Container(
                                                            margin: EdgeInsets.only(left: 10.0),
                                                            child: Text(
                                                                "班级排名:"+snapshot.data[index]["classesRanking"]
                                                            ),
                                                        )
                                                    ),
                                                    Container(
                                                        margin: EdgeInsets.only(right: 10.0),
                                                        child: Text(
                                                            "年级排名:"+snapshot.data[index]["gradeRanking"]
                                                        ),
                                                    )
                                                ],
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(top: 10),
                                                padding: EdgeInsets.all(5.0),
                                                decoration: BoxDecoration(
                                                    color: Color.fromRGBO(97, 204, 245, 0.2),
                                                    border: Border.all(color: Colors.grey, width: 1.0),
                                                    borderRadius: BorderRadius.circular(3.0)
                                                ),
                                                child: Column(
                                                    children: <Widget>[
                                                        Row(
                                                            children: <Widget>[
                                                              Expanded(
                                                                 child: Center(
                                                                  child: Text(
                                                                      "班级平均分"
                                                                  ),
                                                                 )
                                                              ),
                                                              Expanded(
                                                                 child: Center(
                                                                  child: Text(
                                                                      "班级最高分"
                                                                  ),
                                                                 )
                                                              ),
                                                              Expanded(
                                                                 child: Center(
                                                                  child: Text(
                                                                      "班级最低分"
                                                                  ),
                                                                 )
                                                              )
                                                            ],
                                                        ),
                                                        Container(
                                                          height: 5.0,
                                                        ),
                                                        Row(
                                                            children: <Widget>[
                                                              Expanded(
                                                                 child: Center(
                                                                  child: Text(
                                                                      snapshot.data[index]["classesAverage"]
                                                                  ),
                                                                 )
                                                              ),
                                                              Expanded(
                                                                 child: Center(
                                                                  child: Text(
                                                                      snapshot.data[index]["classesMaxResult"]
                                                                  ),
                                                                 )
                                                              ),
                                                              Expanded(
                                                                 child: Center(
                                                                  child: Text(
                                                                      snapshot.data[index]["classesMinResult"]
                                                                  ),
                                                                 )
                                                              )
                                                            ],
                                                        ),
                                                        Container(
                                                          child: Divider(height: 10.0,),
                                                        ),
                                                        Row(
                                                            children: <Widget>[
                                                              Expanded(
                                                                  child: Center(
                                                                    child: Text(
                                                                        "年级平均分"
                                                                    ),
                                                                  )
                                                              ),
                                                              Expanded(
                                                                 child: Center(
                                                                    child: Text(
                                                                        "年级最高分"
                                                                    ),
                                                                 )
                                                              ),
                                                              Expanded(
                                                                  child: Center(
                                                                      child: Text(
                                                                          "年级最低分"
                                                                      ),
                                                                  )
                                                              )
                                                            ],
                                                        ),
                                                        Container(
                                                          height: 5.0,
                                                        ),
                                                        Row(
                                                            children: <Widget>[
                                                              Expanded(
                                                                  child: Center(
                                                                      child: Text(
                                                                          snapshot.data[index]["gradeAverage"]
                                                                      ),
                                                                  ),
                                                              ),
                                                              Expanded(
                                                                  child: Center(
                                                                      child: Text(
                                                                        snapshot.data[index]["gradeMaxResult"]
                                                                      ),
                                                                  ),
                                                              ),
                                                              Expanded(
                                                                  child: Center(
                                                                    child: Text(
                                                                        snapshot.data[index]["gradeMinResult"]
                                                                    ),
                                                                  )
                                                              )
                                                            ],
                                                        )
                                                    ],
                                                ),
                                            )
                                        ],
                                    ),
                                );
                            },
                        );
                    }else{
                        return Container(
                            height: double.infinity,
                            width: double.infinity,
                            child: Center(
                                child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: <Widget>[
                                        Image.asset(
                                            "image/loading2.gif",
                                            scale: 2.0,
                                        ),
                                        Text("加载中···")
                                    ],
                                )
                            ),
                        );
                    }
                },
            ),
        ),
      );
    }


    List<Widget> _sliverBuilder(BuildContext context, bool innerBoxIsScrolled) {
    return <Widget>[
      SliverAppBar(
        centerTitle: true,    //标题居中
        expandedHeight: 200.0,  //展开高度200
        floating: false,  //不随着滑动隐藏标题
        pinned: true,    //固定在顶部
        flexibleSpace: FlexibleSpaceBar(
          centerTitle: true,
          title: Text(
            "$examName",
            style: TextStyle(
              fontSize: 16.0,
            ),
          ),
          background: Image.asset(
            "image/examResultBG.jpg",
            fit: BoxFit.cover,
          ),
        ),
      )
    ];
  }

    void getExamName(){
    Future<dynamic> future = Future(()async{
                SharedPreferences prefs =await SharedPreferences.getInstance();
                return prefs.getString("examName");
            });
            future.then((val){
                setState(() {
                    examName = val;             
                });
            }).catchError((_){
                print("catchError");
            });
  }


    Future getExamResultDetail()async{
            
            var data = {
                "analysisId": widget.analysisId,
                "roleId": widget.roleId,
                "schoolYearStart": widget.schoolYearStart
            };
            return await DioUtil.request(context,'/tqes/web/squaAnalysisExamDetail/getStudentSubjectDetail',formData: data).then((val){
                Map<String,dynamic> responseData = json.decode(val.toString());
                // print(responseData);
                if(responseData["success"]){
                    // setState(() {
                    //     examResultDetail = responseData["data"];
                    // });
                    // print(examResultDetail);
                    return(responseData["data"]);
                }else{
                    OkToast.singleToast(responseData["msg"]);
                    return("加载完成2");
                }
                
            });
    }


}